package com.jsoft.jsofted.platform.service.impl;

import com.jsoft.jsofted.platform.entity.DicEntity;
import com.jsoft.jsofted.platform.mapper.DicMapper;
import com.jsoft.jsofted.framework.mybatis.CocQueryWrapper;
import com.jsoft.jsofted.framework.service.impl.CocServiceImpl;
import com.jsoft.jsofted.platform.service.DicService;
import com.jsoft.jsofted.platform.vo.DicBean;
import com.jsoft.jsofted.platform.vo.DicTreeMaker;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Service
public class DicServiceImpl extends CocServiceImpl<DicMapper, DicEntity> implements DicService {
    @Override
    public List<DicBean> queryTree(Map queryParams) {
        if (queryParams == null) {
            queryParams = new LinkedHashMap();
        }

        CocQueryWrapper queryWrapper = new CocQueryWrapper();
        this._parseSortParams(queryWrapper, queryParams);
        this._parseKeywordsParams(queryWrapper, queryParams);

        List list = list(queryWrapper);
        List tree = DicTreeMaker.make(list);

        return tree;
    }

    private void _parseSortParams(CocQueryWrapper queryWrapper, Map queryParams) {
        queryWrapper.orderByDesc("sort_value", "create_time");
    }

    /**
     * 解析【条件参数】中的【关键字参数】
     */
    private void _parseKeywordsParams(CocQueryWrapper queryWrapper, Map condParams) {
        String keywords = (String) condParams.get("keywords");
        if (keywords == null || keywords.trim().length() == 0) {
            return;
        }

        Consumer<CocQueryWrapper> consumer = qw -> {
            qw.like("name", keywords);
            qw.or();
            qw.like("item_name", keywords);
            qw.or();
            qw.like("item_value", keywords);
            qw.or();
            qw.like("type", keywords);
        };
        queryWrapper.and(consumer);
    }
}
